package me.zhengjie.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import me.zhengjie.domain.SysLog;
import me.zhengjie.modules.system.domain.*;
import me.zhengjie.modules.system.domain.vo.ClazzanddepartmentQueryCriteria;
import me.zhengjie.modules.system.mapper.*;
import me.zhengjie.modules.system.service.ClazzandstudentService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@RequiredArgsConstructor
public class ClazzandstudentServiceImpl extends ServiceImpl<ClazzandstudentMapper, Clazzandstudent> implements ClazzandstudentService {

    private final ClazzandstudentMapper clazzandstudentMapper;

    private final StudentMapper studentMapper;

    private final UserMapper userMapper;

    private final CourseandclazzMapper courseandclazzMapper;

    private final ClazzMapper clazzMapper;

    public List<ClassStudentCount> getClassStudentCounts(List<Integer> classIds){

        return clazzandstudentMapper.getClassStudentCounts(classIds);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<Student> getClazzStudent(Integer clazzId) {

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        String currentUserName = authentication.getName();
        User currentUser = userMapper.findByUsername(currentUserName);
        //获取该学校学生
        QueryWrapper<User> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("dept_id",currentUser.getDeptId());
        List<User> userList = userMapper.selectList(queryWrapper2);

        //获取用户id
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("clazzId",clazzId);
        List<Clazzandstudent> clazzandstudents = clazzandstudentMapper.selectList(queryWrapper);


        List<Long> studentIds = new ArrayList<>();
        for(Clazzandstudent clazzandstudent :clazzandstudents){
            studentIds.add(clazzandstudent.getStudentId());
        }

        QueryWrapper<Student> queryWrapper1 = new QueryWrapper<Student>();
        queryWrapper1.in("id",studentIds);
        if(!studentIds.isEmpty()){
            List<Student> studentList = studentMapper.selectList(queryWrapper1);
//            return studentMapper.selectList(queryWrapper1);
            for(Student student:studentList){
                for(User user:userList){
                    if(Objects.equals(student.getUserid(), user.getId())){
                        student.setUsername(user.getUsername());
                    }
                }
            }
            return studentList;
        } else {
            return new ArrayList<>();
        }

    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    //获取未进入班级学生 进入该课程 未进入班级的学生 传入课程id
    public List<Student> getNotClazzStudent(Integer courseId) {

        //获取该课程所有班级
        QueryWrapper queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("courseId",courseId);
        List<Courseandclazz> courseandclazzes = courseandclazzMapper.selectList(queryWrapper1);
        List<Long> clazzIds = new ArrayList<>();
        for(Courseandclazz courseandclazz :courseandclazzes){
            clazzIds.add(courseandclazz.getClazzId());
        }

        //获取该课程存在的学生
        QueryWrapper queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.in("clazzId",clazzIds);
        List<Clazzandstudent> clazzandstudents = clazzandstudentMapper.selectList(queryWrapper2);
        List<Long> studentAleardIds = new ArrayList<>();
        for(Clazzandstudent clazzandstudent :clazzandstudents){
            studentAleardIds.add(clazzandstudent.getStudentId());
        }


        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        String currentUserName = authentication.getName();
        User currentUser = userMapper.findByUsername(currentUserName);
        //获取该学校学生
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dept_id",currentUser.getDeptId());
        List<User> userList = userMapper.selectList(queryWrapper);

        if(userList.size()>0){
            List<Long> userIds = new ArrayList<>();
            for(User user :userList){
                userIds.add(user.getId());
            }
            QueryWrapper queryWrapper3 = new QueryWrapper<Student>();
            queryWrapper3.in("userid",userIds);
            List<Student> studentList = studentMapper.selectList(queryWrapper3);
            List<Student> resultList = new ArrayList<>();
            for(Student student : studentList){
                if(!containsElement(studentAleardIds,student.getId())){
                    resultList.add(student);
                }
            }
            for(Student student:resultList){
                for(User user:userList){
                    if(student.getUserid() == user.getId()){
                        student.setUsername(user.getUsername());
                    }
                }
            }
            return resultList;
        }
        return null;
    }

    public List<Student> getNotClazzStudentList(ClazzanddepartmentQueryCriteria criteria){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        String currentUserName = authentication.getName();
        User currentUser = userMapper.findByUsername(currentUserName);
        //获取该学校学生
        QueryWrapper<User> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("dept_id",currentUser.getDeptId());
        List<User> userList = userMapper.selectList(queryWrapper2);

        //获取用户id
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("clazzId",criteria.getClazzId());
        List<Clazz> clazzList = clazzMapper.selectList(queryWrapper);

        //获取班级id组
        List<Integer> clazzIdList = new ArrayList<>();
        for(Clazz clazz:clazzList){
            clazzIdList.add(Math.toIntExact(clazz.getId()));
        }
        List<Clazzandstudent> clazzandstudentList = new ArrayList<>();
        if(clazzIdList.size()!=0){
            QueryWrapper queryWrapper1 = new QueryWrapper<>();
            queryWrapper.in("clazzId",clazzIdList);
            clazzandstudentList = clazzandstudentMapper.selectList(queryWrapper);
        }

        List<Integer> studentIds = new ArrayList<>();
        for(Clazzandstudent clazzandstudent :clazzandstudentList){
            studentIds.add(Math.toIntExact(clazzandstudent.getStudentId()));
        }

        //获取本校所有学生
        QueryWrapper queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.in("schoolId",currentUser.getDeptId());
        List<Student> allStudentList = studentMapper.selectList(queryWrapper1);
        for(int i = 0;i<allStudentList.size();i++){
            for(Integer item:studentIds){
                if(Math.toIntExact(allStudentList.get(i).getId()) == item){
                    allStudentList.remove(i);
                }
            }
        }
        return allStudentList;
    }


    public boolean containsElement(List<Long> array, Long target) {
        for (int i = 0; i < array.size(); i++) {
            if (array.get(i) ==  target) {
                return true;
            }
        }
        return false;
    }

    //绑定班级学生

}
